package com.muyu.system.service.impl;


import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.DateUtils;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.system.domain.DanmoShoppingReviews;
import com.muyu.system.domain.DanmoShoppingReviewsPicture;
import com.muyu.system.mapper.DanmoShoppingReviewsMapper;
import com.muyu.system.req.DanmoShoppingReviewsDto;
import com.muyu.system.service.IDanmoShoppingReviewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 产品评论Service业务层处理
 *
 * @author yangle
 * @date 2024-09-29
 */
@Service
public class DanmoShoppingReviewsServiceImpl implements IDanmoShoppingReviewsService {
    @Autowired
    private DanmoShoppingReviewsMapper danmoShoppingReviewsMapper;

    /**
     * 查询产品评论
     *
     * @param id 产品评论主键
     * @return 产品评论
     */
    @Override
    public DanmoShoppingReviews selectDanmoShoppingReviewsById(Long id) {
        return danmoShoppingReviewsMapper.selectDanmoShoppingReviewsById(id);
    }

    /**
     * 查询产品评论列表
     *
     * @param danmoShoppingReviews 产品评论
     * @return 产品评论
     */
    @Override
    public Result selectDanmoShoppingReviewsList(DanmoShoppingReviews danmoShoppingReviews) {
        Long type = danmoShoppingReviews.getType();
        danmoShoppingReviews.setType(null);
        List<DanmoShoppingReviews> danmoShoppingReviewsFinal = danmoShoppingReviewsMapper.selectDanmoShoppingReviewsList(danmoShoppingReviews);
        DanmoShoppingReviewsDto danmoShoppingReviewsDto = new DanmoShoppingReviewsDto();
        Map<String, Long> countMap = new HashMap<>();
        // 好评数量
        countMap.put("好评", 0L);
        // 含图评论数量
        countMap.put("图/视频", 0L);
        // 追评数量
        countMap.put("追评", 0L);
        // 中评数量
        countMap.put("中评", 0L);
        // 差评数量
        countMap.put("差评", 0L);
        for (DanmoShoppingReviews shoppingReviews : danmoShoppingReviewsFinal) {
            // 分类
            if (shoppingReviews.getFatherCommentId() != null) {
                countMap.put("追评", countMap.get("追评") + 1);
            }
            if (shoppingReviews.getDanmoShoppingReviewsPictureList().size() != 0) {
                countMap.put("图/视频", countMap.get("图/视频") + 1);
            }
            if (shoppingReviews.getRating() > 3) {
                countMap.put("好评", countMap.get("好评") + 1);
                continue;
            }
            if (shoppingReviews.getRating() == 3) {
                countMap.put("中评", countMap.get("中评") + 1);
                continue;
            }
            if (shoppingReviews.getRating() < 3) {
                countMap.put("差评", countMap.get("差评") + 1);
            }
        }
        danmoShoppingReviews.setType(type);
        danmoShoppingReviewsFinal = danmoShoppingReviewsMapper.selectDanmoShoppingReviewsList(danmoShoppingReviews);
        if (danmoShoppingReviews.getType() != null && danmoShoppingReviews.getType() == 2){
            List<DanmoShoppingReviews> danmoShoppingReviews1 = new ArrayList<>();
            for (DanmoShoppingReviews shoppingReviews : danmoShoppingReviewsFinal) {
                if (shoppingReviews.getDanmoShoppingReviewsPictureList().size() != 0){
                    danmoShoppingReviews1.add(shoppingReviews);
                }
            }
            danmoShoppingReviewsFinal = danmoShoppingReviews1;
        }
        danmoShoppingReviewsDto.setDanmoShoppingReviewsList(danmoShoppingReviewsFinal);
        danmoShoppingReviewsDto.setCountMap(countMap);
        return Result.success(danmoShoppingReviewsDto);
    }

    /**
     * 新增产品评论
     *
     * @param danmoShoppingReviews 产品评论
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDanmoShoppingReviews(DanmoShoppingReviews danmoShoppingReviews) {
        danmoShoppingReviews.setCreateTime(DateUtils.getNowDate());
        int rows = danmoShoppingReviewsMapper.insertDanmoShoppingReviews(danmoShoppingReviews);
        insertDanmoShoppingReviewsPicture(danmoShoppingReviews);
        return rows;
    }

    /**
     * 修改产品评论
     *
     * @param danmoShoppingReviews 产品评论
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDanmoShoppingReviews(DanmoShoppingReviews danmoShoppingReviews) {
        danmoShoppingReviews.setUpdateTime(DateUtils.getNowDate());
        danmoShoppingReviewsMapper.deleteDanmoShoppingReviewsPictureByReviewsId(danmoShoppingReviews.getId());
        insertDanmoShoppingReviewsPicture(danmoShoppingReviews);
        return danmoShoppingReviewsMapper.updateDanmoShoppingReviews(danmoShoppingReviews);
    }

    /**
     * 批量删除产品评论
     *
     * @param ids 需要删除的产品评论主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDanmoShoppingReviewsByIds(Long[] ids) {
        danmoShoppingReviewsMapper.deleteDanmoShoppingReviewsPictureByReviewsIds(ids);
        return danmoShoppingReviewsMapper.deleteDanmoShoppingReviewsByIds(ids);
    }

    /**
     * 删除产品评论信息
     *
     * @param id 产品评论主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDanmoShoppingReviewsById(Long id) {
        danmoShoppingReviewsMapper.deleteDanmoShoppingReviewsPictureByReviewsId(id);
        return danmoShoppingReviewsMapper.deleteDanmoShoppingReviewsById(id);
    }

    /**
     * 新增评论图片信息
     *
     * @param danmoShoppingReviews 产品评论对象
     */
    public void insertDanmoShoppingReviewsPicture(DanmoShoppingReviews danmoShoppingReviews) {
        List<DanmoShoppingReviewsPicture> danmoShoppingReviewsPictureList = danmoShoppingReviews.getDanmoShoppingReviewsPictureList();
        Long id = danmoShoppingReviews.getId();
        if (StringUtils.isNotNull(danmoShoppingReviewsPictureList)) {
            List<DanmoShoppingReviewsPicture> list = new ArrayList<DanmoShoppingReviewsPicture>();
            for (DanmoShoppingReviewsPicture danmoShoppingReviewsPicture : danmoShoppingReviewsPictureList) {
                danmoShoppingReviewsPicture.setReviewsId(id);
                list.add(danmoShoppingReviewsPicture);
            }
            if (list.size() > 0) {
                danmoShoppingReviewsMapper.batchDanmoShoppingReviewsPicture(list);
            }
        }
    }
}
